<html>
<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="weaver.common.DateUtil" %>
<%@page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.Util" %>
<%
    String curdate = DateUtil.getFullDate();
    int nodeid = Util.getIntValue(request.getParameter("nodeid"));//流程的节点id
    RecordSet rst = new RecordSet();
    String isstart = "0";
    rst.executeQuery("select isstart from WORKFLOW_NODEBASE where id=" + nodeid);//工作流节点信息表中的当前节点是否为创建节点字段
    if (rst.next()) {
        isstart = Util.null2String(rst.getString("isstart"));
    }
%>

<head>
</head>
<body>
</body>
<script type="text/javascript">
    var zsdata = "0";
    var index = 0;
    var isstart = '<%=isstart%>';//1为创建节点
    var nodeid = '<%=nodeid%>';
    var datetime = '<%=curdate%>';
    var zs_fieldid_dt1 = WfForm.convertFieldNameToId("zs", "detail_1");//桌数
    var lkrs_fieldid_dt1 = WfForm.convertFieldNameToId("lkrs", "detail_1");//来客人数
    var ptrs_fieldid_dt1 = WfForm.convertFieldNameToId("ptrs", "detail_1");//陪同人数
    var jcrq_fieldid_dt1 = WfForm.convertFieldNameToId("jcrq", "detail_1");//聚餐日期
    var yclx_fieldid_dt1 = WfForm.convertFieldNameToId("yclx", "detail_1");//用餐类型
    var zrs_fieldid_dt1 = WfForm.convertFieldNameToId("zrs", "detail_1");//总人数
    jQuery(document).ready(function () {

        var calval = parseInt(5+(value*1.0-5)/2);
        if(value*1.0>calval){
            alert("来客人数超过5人的，超过部分陪同人数不超过来客人数的二分之一");
         WfForm.changeFieldValue(ptrs_fieldid_dt1 + "_" + rowIndex, {value: ""});


        /**
         * 1、总桌数，用于判断、统计明细每天（jcrq）同一用餐类型（yclx）的所有明细行桌数字段（zs）之和不能大于3，
         * 若大于则提示：“每餐每天申请不得超过3桌！”
         */
        WfForm.bindDetailFieldChangeEvent(jcrq_fieldid_dt1+","+yclx_fieldid_dt1, function (id, rowIndex, value) {
           if(value !='' && value.split(',').length>2){
               var newarr = [];
               var dt1 = WfForm.getDetailAllRowIndexStr("detail_1").split(",");
               for (var i = 0; i < dt1.length; i++) {
                   var jcrq_fieldvalue = WfForm.getFieldValue(jcrq_fieldid_dt1 + "_" + dt1[i]);
                   var yclx_fieldvalue = WfForm.getFieldValue(yclx_fieldid_dt1 + "_" + dt1[i]);
                   newarr.push(jcrq_fieldvalue+","+yclx_fieldvalue);
               }
               if(hasEleWithThanThreeRepeats(newarr)){
                   weaJs.alert("每餐每天申请不得超过3桌！");
                   WfForm.changeFieldValue(zs_fieldid_dt1 + "_" + rowIndex, {value: ''});
               }
           }

        });
        /**
         * 2、明细中桌数为1时控制总人数（zrs）不能大于13，若大于则提示：“每桌最多容纳13人就餐！”；
         桌数为2时控制总人数（zrs）不能大于20，若大于则提示：“每桌最多容纳13人就餐！”；
         桌数为3时控制总人数（zrs）不能大于30，若大于则提示：“每桌最多容纳13人就餐！”；
         */

        WfForm.bindDetailFieldChangeEvent(zrs_fieldid_dt1, function (id, rowIndex, value) {
            if(value !=''){
                if(parseInt(value)>30){
                    weaJs.alert("每桌最多容纳13人就餐！");
                    WfForm.changeFieldValue(zs_fieldid_dt1 + "_" + rowIndex, {value: ''});
                }else {
                    var newarr = [];
                    var dt1 = WfForm.getDetailAllRowIndexStr("detail_1").split(",");
                    for (var i = 0; i < dt1.length; i++) {
                        var jcrq_fieldvalue = WfForm.getFieldValue(jcrq_fieldid_dt1 + "_" + dt1[i]);
                        var yclx_fieldvalue = WfForm.getFieldValue(yclx_fieldid_dt1 + "_" + dt1[i]);
                        newarr.push(jcrq_fieldvalue+","+yclx_fieldvalue);
                    }
                    var zrs = parseInt(value);
                    if(0< zrs && zrs <= 13){
                        WfForm.changeFieldValue(zs_fieldid_dt1 + "_" + rowIndex, {value: 1});
                    }else if(13< zrs && zrs <= 26){
                        WfForm.changeFieldValue(zs_fieldid_dt1 + "_" + rowIndex, {value: 2});
                    }else if(26< zrs && zrs <= 39){
                        WfForm.changeFieldValue(zs_fieldid_dt1 + "_" + rowIndex, {value: 3});
                    }else {
                        weaJs.alert("每桌最多容纳13人就餐！");
                    }
                }
            }

        });




    })


    //4、明细行总人数必须大于0，否则提示：“总人数为0时，无法提交，请检查”；
    function checkSubmitZrs(isstart) {
        var flag = true;
        if (isstart == "1") {
            var dt1 = WfForm.getDetailAllRowIndexStr("detail_1");
            for (var i = 0; i < dt1.length; i++) {
                var zrs_fieldvalue = WfForm.getFieldValue(zrs_fieldid_dt1 + "_" + dt1[i]);
                if (zrs_fieldvalue * 1.0 <= 0) {
                    alert("总人数为0时，无法提交，请检查");
                    flag = false;
                    continue;
                }
            }
        }
        return flag;
    }

    //判断数组元素重复次数是否超过3次
    function hasEleWithThanThreeRepeats(arr) {
        let counts = {};

        for (let item of arr) {
            if (counts[item]) {
                counts[item]++;
            } else {
                counts[item] = 1;
            }
        }

        return Object.keys(counts).some(key => counts[key] > 3);
    }

    //查找重复元素
    function findDuplicatesUsingFilter(arr) {
        let duplicates = [];

        arr.forEach((item) => {
            if (arr.indexOf(item) !== arr.lastIndexOf(item) && !duplicates.includes(item)) {
                duplicates.push(item);
            }
        });

        return duplicates;
    }

</script>
</html>